package com.wang.servlet;

import com.wang.pojo.Person;
import sun.security.jca.ServiceId;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class Session01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //解决编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //输出对象方法
        PrintWriter writer = resp.getWriter();

        //得到session
        HttpSession session = req.getSession();
        session.setAttribute("name",new Person("Q",1));        //给Session设置一些值

        String sessionId = session.getId();

        //判断session是否存在
        if(session.isNew()){
            resp.getWriter().write("session创建成功，I"+sessionId);
        }else{
            resp.getWriter().write("session以及服务器中存在了，Id"+sessionId);
        }




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
